var salt = sessionStorage.getItem('usersalt');
var box = /\s/g;
$('#submitsev').on('click', function() {
	addrisk();
});
var arr = new Array();
var arr2 = new Array();
var danweim = '';

/*-------------新增隐患-------------*/
function addrisk() {

	var findpos = $.trim($('#findpos').val()); //发现人
	if(findpos) {

	}
	var findtime = $.trim($('#findtime').val()); //发现时间
	var risk_type = $('#risk_type').val(); //隐患类型

	var risk_descr = $.trim($('#risk_descr').val()); //隐患描述
	var addpic = ''; //添加照片
	var checkone = $.trim($('#checkone').val()); //核查人
	var checktime = $('#checktime').val(); //核查时间
	var risk_endtime = $('#risk_endtime').val(); //防控措施完成时间
	var checkverdict = $.trim($('#checkverdict').val()); //核查结论
	var prevention = $.trim($('#prevention').val()); //防控措施
	var endperiod = $('#test-limit2').val() //治理期限
	var dutyperson = $.trim($('#dutyperson').val()); //责任人
	//	var dutycompay=$.trim($('#dutycompay').val());//责任单位
	var dutycompay = getval().toString(); //责任单位
	//		dutycompay=dutycompay.toString()
	if(dutycompay == 0) {
		dutycompay = '';

	}
	if(findpos != '' && risk_type != '' && risk_descr != '' && checkone != '' && checktime != '' && risk_endtime != '' && checkverdict != '' && prevention != '' && endperiod != '') {
		$.ajax({
			url: url + 'perilInfo/add',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt,
				findUsers: findpos,
				findTime: findtime,
				typeId: risk_type,
				describe: risk_descr,
				auditUser: checkone,
				auditTime: checktime,
				auditFruit: checkverdict,
				auditStep: prevention,
				endTime: endperiod,
				leaderUser: dutyperson,
				leaderUnits: dutycompay,
				attIds: arr.toString(),
				finishTime: risk_endtime

			},
			success: function(data) {
				if(data.code == 0) {
					layer.confirm('隐患填报提交成功！', {
						btn: ['确定'] //按钮
					}, function() {
						location.href = "../risk-list/risk-list.html";
					}, function() {

					});
				} else {
					layer.msg(data.msg);
				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});
	} else {
		layer.msg('请填写完整');
	}

}
$('#updatapic').on('click', function() {
	$("#selfile").click();
});

/*删除事件*/
function delpic(self, num, inde) {
	layer.confirm('你确定要删除吗？', {
		btn: ['确定', '取消'] //按钮
	}, function() {
		$('#' + self).remove();
		for(var i = 0; i < arr.length; i++) {
			if(arr[i] == inde) {
				arr.splice(i, 1);
			}
		}

		layer.msg('删除成功');
	}, function() {

	});
}

function lookbigpic(self) {

	//	$.getJSON('test/photos.json?v=' + new Date, function(json) {
	var json = {
		"status": 1,
		"msg": "",
		"title": "",
		"id": 8,
		"start": 0,
		"data": [{
			"alt": "",
			"pid": 109,
			"src": url + "attachment/seekFile/" + self,
			"thumb": ""
		}]
	}
	layer.photos({
		photos: json,
		anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
	});
	//	});
	//	layer.open({
	//		type: 1,
	//		title: false,
	//		closeBtn: 0,
	//		shadeClose: true,
	//		//		area: '516px',
	//		//	  skin: 'layui-layer-rim', //加上边框
	//		area: ['516px', '90%'], //宽高
	//		content: '<div style="text-align:center"><img src="' + url + 'attachment/seekFile/' + self + '" style="width: auto;height: auto;max-width: 100%;max-height: 100%;" /></div>'
	//	});

}

$('#resclas').click(function() {
	$('#resclass').show();
	$('#resclas').val('')
	$.fn.zTree.init($("#resclass"), setting, zNodes);
});

var company;

getallent();

function getallent() {

	$.ajax({
		url: url + 'baseEnt/getAllEnt',
		dataType: 'json',
		async: false,
		type: "POST",
		data: {

		},
		success: function(data) {
			$('#resclass').empty();
			if(data.code == 0) {
				var list = data.data;
				company = list;

			}

		},
		error: function(error) {
			console.log("获取失败");
		}
	});

}

var setting = {
	check: {
		enable: true
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	check: {
		enable: true,
		chkStyle: "radio", //单选框
		radioType: "all" //对所有节点设置单选
	},
	callback: {
		onCheck: zTreeOnCheck
	}

};

var zNodes = company;

$.fn.zTree.init($("#resclass"), setting, zNodes);

$('#getval').click(function() {
	getval();
})

function zTreeOnCheck(event, treeId, treeNode) {
	var checked = treeNode.checked;
	danweim = treeNode ? treeNode.name : "root"
	$('#resclas').val(danweim)
	$('#resclass').hide();

};

function getval() {
	arr2 = [];
	danweim = '';
	var treeObj = $.fn.zTree.getZTreeObj("resclass");
	var nodes = treeObj.getCheckedNodes(true),

		v = "";
	for(var i = 0; i < nodes.length; i++) {
		v += nodes[i].name + ",";

		arr2.push(nodes[i].id); //获取选中节点的值
	}
	return arr2;
}

layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laydate = layui.laydate,
		laytpl = layui.laytpl,
		table = layui.table;
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);

	}
	//	$('#picapp').on('click', function() {
	//		layer.photos({
	//			photos: '#picupdata1',
	//			anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
	//		});
	//	});

	//前后若干天可选，这里以7天为例
	laydate.render({
		elem: '#findtime',
		trigger: 'click',
		max: 0
	});

	laydate.render({
		elem: '#checktime',
		trigger: 'click'
	});

	laydate.render({
		elem: '#risk_endtime',
		trigger: 'click'
	});

	laydate.render({
		elem: '#test-limit2',
		trigger: 'click'

	});
	/*上传图片 */
	var flg = 0;

	layui.use('upload', function() {
		var $ = layui.jquery,
			upload = layui.upload;

		upload.render({
			elem: '#test2',
			url: url + 'attachment/upload',
			multiple: true,
			accept: 'file',
			exts: 'png|jpg|gif|jpeg|PNG|JPG|GIF|JPEG',
			before: function(obj) {},
			done: function(res) {
				//上传完毕
				if(res.code == 0) {
					flg++;
					arr.push(res.data.id);

					var img = $('<div style="display:inline-block;position:relative;margin-right:20px;" id="picupdata' + flg + '"  ><div onclick="delpic(\'picupdata' + flg + '\',\'' + flg + '\',\'' + res.data.id + '\')" style="position:absolute;top:-4px;right:-5px;color:white;background:red;border:1px solid red;border-radius:50%;width:16px;height:16px"><i class="layui-icon" style="position:relative;top:-2px;right:0px;">&#x1006;</i></div><img id="pic' + res.data.id + '" onclick="lookbigpic(\'' + res.data.id + '\')" src="' + url + 'attachment/seekFile/' + res.data.id + '" width="100px" height="100px" class="picsty"/></div>');
					$('#picapp').append(img);

				}
			}
		});

	});
	/*隐患类型*/
	risktype();

	function risktype() {
		$.ajax({
			url: url + 'perilInfo/getAllType',
			dataType: 'json',
			async: false,
			type: "POST",
			data: {
				Token: salt
			},
			success: function(data) {
				$('#risk_type').empty();
				if(data.code == 0) {
					var list = data.data;
					var opt = '<option value="">请选择</option>';
					for(var i = 0; i < list.length; i++) {
						opt += '<option value="' + list[i].id + '">' + list[i].name + '</option>'
					}
					$('#risk_type').append(opt);
					form.render();
				}

			},
			error: function(error) {
				console.log("获取失败");
			}
		});

	}

	//*获取所有企业*/
	//     riskcompay();
	//     function riskcompay(){
	//     		$.ajax({
	//			url: url+'baseEnt/getAllEnt',
	//	      		dataType: 'json',
	//	      		async: false,
	//	      		type: "POST",
	//	      		data:{
	//	      			Token:salt
	//	      		},
	//	      		success: function(data) {
	//	      			$('#dutycompay').empty();
	//	      			if(data.code==0){
	//	      				var list=data.data;
	//	      				var opt='<option value="0">请选择</option>'
	//	      				for(var i=0;i<list.length;i++){
	//	      					opt+='<option value="'+list[i].id+'" >'+list[i].name+'</option>'
	//	      				}
	//	      				$('#dutycompay').append(opt);
	//	      				form.render();
	//	      			}
	//	
	//	      		},
	//	      		error: function(error) {
	//	      			console.log("获取失败");
	//	      		}
	//	      	});
	//     }
	//      

});